﻿using System;

namespace WindowsPhoneMvp.Phone
{
    public static class MvpPhoneApplicationPageExtensions
    {
        /// <summary>
        /// Returns the Model for this View
        /// </summary>
        public static TModel Model<TModel>(this IView<TModel> view) where TModel : class, new()
        {
            if(view.DataContext == null)
                throw new InvalidOperationException("The Model property is currently null, however it should have been automatically initialized by the presenter. This most likely indicates that no presenter was bound to the control. Check your presenter bindings.");

            return (TModel)view.DataContext;
        }
        /// <summary>
        /// Sets the Model for this View
        /// </summary>
        public static void Model<TModel>(this IView<TModel> view, TModel setModel) where TModel : class, new()
        {
            view.DataContext = setModel;
        }
    }
}
